/*****************************************************************************
 * Copyright (c) 2022, Nations Technologies Inc.
 *
 * All rights reserved.
 * ****************************************************************************
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Nations' name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ****************************************************************************/

/**
 * @file main.c
 * @author Nations
 * @version V1.2.2
 *
 * @copyright Copyright (c) 2022, Nations Technologies Inc. All rights reserved.
 */
#include "main.h"
#include <stdio.h>
#include <stdint.h>

#include "sys_param.h"
#include "my_uart.h"
#include "my_power.h"
#include "my_basic_timer.h"
#include "HF_pwm_drv.h"
#include "HF_power.h"
#include "my_dac.h"
#include "EMS_pwm_drv.h"
#include "EMS_power.h"
#include "my_buzzer.h"
#include "my_motor.h"
#include "my_key_drv.h"
#include "rgb_spi_drv.h"
#include "my_adc_drv.h"
#include "my_battery.h"
#include "my_output_wave.h"
#include "soft_iic_drv.h"
#include "my_ntc_app.h"
#include "my_err_check.h"
#include "my_iwdt.h"
#include "my_uart_app.h"
#include "zhan_zhi_dao.h"
#include "my_flash.h"


//软件版本命名规则: JE3_MAIN_N32L406CBQ7_V0.0.1_20241204
uint8_t soft_ver_str[20] = "V0.0.2.0";

// 使用开发板调试
// #define USE_BOARD_DEBUG        0


/**
 * @brief  Inserts a delay time.
 * @param count specifies the delay time length.
 */
void Delay(uint32_t count)
{
    for (; count > 0; count--)
        ;
}





/**
 * @brief Assert failed function by user.
 * @param file The name of the call that failed.
 * @param line The source line number of the call that failed.
 */
#ifdef USE_FULL_ASSERT
void assert_failed(const uint8_t* expr, const uint8_t* file, uint32_t line)
{
    while (1)
    {
    }
}
#endif // USE_FULL_ASSERT

/**
 * @brief  Main program.
 */

extern SYS_PARAM_t sys_data;

int main(void)
{
    /*SystemInit() function has been called by startup file startup_n32l40x.s*/
    int task_1ms_cnt = 0 ;
    int task_20ms_cnt = 0;
    static int hf_ctrl_time = 0;
    int test_duty;
    int ems_duty_change = 0;
    int led_run_cnt = 0;
	int rgb_reset_flag = 0;
    int gsener_read_delay = 0;
    int printf_delay = 0;

//--------------system init begin--------
	my_basic_timer_init();
	sys_power_init();	
	my_battery_init();		// 关闭充电使能后，接入5v充电器，电流为12ma
    //	my_hf_init();
    my_hf_power_init();
    my_dac_init();
    my_ems_power_init();

    my_motor_init();
    my_rgb_init();
//	
    my_uart_init();
    ems_power_off();
    my_adc_init();

    my_lllt_init();
    my_key_init();
    
    // printf("JE3 app  sys run --\r\n");

     set_sys_state(SLEEP_STA);

    set_sys_rgb_color(BLACK_COLOR);
    set_sys_level(LEVEL_0);
    printf("\r\n JE3 APP soft_ver:[%s] \r\n", soft_ver_str);
    buzzer_only_close();
    sys_data.sys_power_off_flag = 0;
    sys_data.idle_sta_cnt = 0;
		
    soft_iic_init(); 
    SC7A20_Init();
	task_1ms_cnt = 0;	
    my_iwdt_init();
    my_zhanzhidao_init();	
//    //------------在开发板上运行时开启
#ifdef USE_BOARD_DEBUG
	   set_sys_mode(FACE_MODE);
	   set_sys_state(NORMAL_WORK_STA);
	   my_ems_init();
	   my_hf_init();
	   set_sys_level(2);
#endif
    //------------在开发板上运行时开启

    while (1)
    {
        if(sys_data.task_1ms_flag){
            task_1ms_cnt ++;
        
            if(task_1ms_cnt%5 == 0){
                my_key_scanf();
            }
            /********************************************/
            // 将1ms定时器，规划 20ms是一个大周期
            // 将任务 分在不同的时间段执行
            /*******************************************/
             my_output_ctrl();    // 每1ms 波形输出
            switch(task_1ms_cnt){
               case 1:{ 
                buzzer_state_ctrl();
                break;
                }     
               case 3:{
                motor_state_ctrl();
                my_zhanzhidao_scanf();
                break;
                }
               case 5:{
                // my_ntc_scanf();

                // if(sys_data.key_long_check_cnt){
                //     sys_data.key_long_check_cnt --;
                //     if(0x03 == sys_data.key_long_data){
                //         printf("double long key \r\n");
                //     }
                // }
                break;
                }
               case 7:{
#ifndef USE_BOARD_DEBUG                   
                my_battery_scanf();
#endif                
                break;
               }
               case 9:{
                sys_color_select();
                my_lllt_disp();
                break;
               }
               case 11:{
                rgb_sip_display();
                break;
               }
                case 13:{
                my_err_check();
                sys_sta_check();
                break;
               }
               case 15:{
                gsener_read_delay ++;
                if(gsener_read_delay%5 == 0){
                    g_sensor_read();
                }
                 // 系统开机---power on  2024 --开关机时间要就响应及时
                if(sys_data.sys_power_on_flag){
                    sys_data.sys_power_on_flag = 0;
                    my_ems_init();
                    my_hf_init();
                    sys_power_on();
                    my_buzzer_init(); //蜂鸣器初始化
                    my_flash_data_init();
                    if(ALIGN_STA != get_sys_state())
                    {    
                        if(0 == get_sys_bat_lowest_flag()){
                            motor_shor_on();
                            buzzer_on_with_param(3, 5, 3);
                            set_sys_state(SYS_IDLE_STA);
                        }  
                    }
                    set_sys_mode(FACE_MODE);
                    // set_sys_rgb_color(GREEN_COLOR);
                    // rgb_sip_display();
                    printf("----power on -----\r\n");
                }

                //系统关机--power off
                if(sys_data.sys_power_off_flag){
                    sys_data.sys_power_off_flag = 0;
                    motor_shor_on();
                    buzzer_on_with_param(3, 5, 3);
                    set_sys_rgb_color(BLACK_COLOR);
                    rgb_sip_display();
                    set_sys_level(LEVEL_0);
                    set_sys_mode(FACE_MODE);
                }
                if((sys_data.sta == SLEEP_STA)){       
                    // 自动关机 --统一为蜂鸣器响三声+马达震动后关机
                    // if(sys_data.auto_sleep_flag){
                    //     if(sys_data.motor_off_flag){
                    //         printf("----power off2-----\r\n");
                    //         sys_power_off();
                    //     }
                    // }
                    // else 
                    if(sys_data.beep_finish_flag){
                        // 按键关机
                        my_flash_param_write();
                        printf("----power off1-----\r\n");
                        sys_power_off();
                        sys_data.beep_finish_flag = 0;
                    }
                }
                
                break;
               }
               
              case 20:{
                my_uart_callback();
                my_uart_timeout_checkout();
                break;
               }
               default:{

                break;
               }

            }
            if(task_1ms_cnt >= 20){
                task_1ms_cnt = 0;
                task_20ms_cnt ++; 
								
                if(0 == (task_20ms_cnt%50)){
                    my_iwdt_feed(); // 喂狗----
                    // task_20ms_cnt = 0;
                    

                    // printf("\r\n load_sta:[%d] \r\n", get_sys_load_state());
                    // printf("\r\n err_sta:[%d] \r\n", get_sys_err_mode()); 
                    // printf("\r\n color:[%d] \r\n", get_sys_rgb_color());
                    // printf("gsensor_flag:[%d]\r\n", get_gsensor_flag());
#ifdef DEBUG_OPEN_PRINT                    
                    printf("work_sta:[%d] ", get_sys_state());
                    printf("aling_run_cnt:[%d]\r\n", sys_data.aling_run_cnt);
#endif                    
                    // printf("online_flag: [%d] ", sys_data.cur_mode_active_online_flag);
                    // printf("cur_mode_run:[%d], total:[%d] \r\n", get_cur_mode_run_cnt(), sys_data.run_time);
                    // printf("work_sta:[%d],mode:[%d] ", get_sys_state(), get_sys_mode());
                }
            }
            sys_data.task_1ms_flag = 0;
        }

        if(sys_data.task_1s_flag){
            // printf("sys_data.run_time: [%d] ", sys_data.run_time);
            // printf("load_adc: [%d]\r\n ", get_load_adc());
            // printf("base_connect_flag: [%d]\r\n ", get_base_connect_flag());
            

            // printf("bat_vol: [%d]mV\r\n ", get_sys_battery_voltage());
            // 仅在max模式用到gsensor，如果该模式下gsensor异常，则第一颗灯闪烁进行提示
            if(MAX_MODE == get_sys_mode()){
                if(gsensor_id_check()){
                    // 置1，
                    sys_data.err_mode |= GSENSOR_ID_ERR; 
                }
                else{
                    // 清0
                    sys_data.err_mode &= ~GSENSOR_ID_ERR; 
                }
            }
            
            
            if(ALIGN_STA == get_sys_state()){
                sys_data.aling_run_cnt ++;
                send_aling_mode_sta();
            }
            else{
                uart_send_normal_mode_sta();
            }

            sys_data.task_1s_flag = 0;
            sys_data.run_time ++;   // 总的运行时间++
                                //------------------进入关机------------
  //    1.3 待机：无任何操作超过180秒，自动关闭仪器。
  //    1.4 强制关机：累计开机达到30分钟，则自动判定进行强制关机，如需使用需重新开机使用。          
#ifndef USE_BOARD_DEBUG     //开发板调试时关闭这两项       
           sys_auto_enter_idle_sta_check();
           sys_auto_power_off_check();
#endif
            if(sys_data.run_time%10 == 0){
                printf("work_sta:[%d],mode:[%d] ", get_sys_state(), get_sys_mode());
            }
        }
     
    }


}

/**
 * @}
 */
